/*
**************************************************************************************
*Myriad Engine                                                                       *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
package leola.frontend.parsers;

import java.util.EnumSet;

import leola.ast.ASTNode;
import leola.ast.Expr;
import leola.ast.IfStmt;
import leola.ast.Stmt;
import leola.frontend.LeolaParser;
import leola.frontend.Token;
import leola.frontend.tokens.LeolaTokenType;

/**
 * @author Tony
 *
 */
public class IfStmtParser extends StmtParser {

	// Synchronization set for THEN.
    private static final EnumSet<LeolaTokenType> THEN_SET =
        StmtParser.STMT_START_SET.clone();
    static {        
        THEN_SET.addAll(StmtParser.STMT_FOLLOW_SET);
    }
	
	/**
	 * @param parser
	 */
	public IfStmtParser(LeolaParser parser) {
		super(parser);
	}
	
	/* (non-Javadoc)
	 * @see leola.frontend.parsers.StmtParser#parse(leola.frontend.Token)
	 */
	@Override
	public ASTNode parse(Token token) throws Exception {
		token = nextToken();  // consume the IF
		
		IfStmt ifStmt = null;
		
        // Parse the expression.
        // The IF node adopts the expression subtree as its first child.
        ExprParser expressionParser = new ExprParser(this);
        Expr exprNode = (Expr)expressionParser.parse(token);
        
        // Synchronize at the THEN.
        token = synchronize(THEN_SET);
        /*if (token.getType() == LeolaTokenType.LEFT_BRACE) {
            token = nextToken();  // consume the {
        }
        else {            
            getExceptionHandler().errorToken(token, this, LeolaErrorCode.MISSING_LEFT_BRACE);
        }*/

        // Parse the THEN statement.
        // The IF node adopts the statement subtree as its second child.
        StmtParser statementParser = new StmtParser(this);
        Stmt thenStmt = (Stmt)statementParser.parse(token);
        token = currentToken();

        // Look for an ELSE.
        if (token.getType() == LeolaTokenType.ELSE) {
            token = nextToken();  // consume the ELSE

            // Parse the ELSE statement.
            // The IF node adopts the statement subtree as its third child.
            Stmt elseStmt = (Stmt)statementParser.parse(token);
            ifStmt = new IfStmt(exprNode, thenStmt, elseStmt);
        }
        else {
        	ifStmt = new IfStmt(exprNode, thenStmt);
        }

        setLineNumber(ifStmt, currentToken());
        return ifStmt;
	}

}
